package com.yanyu.space.codegen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanyu.space.codegen.bean.po.Attr;
import com.yanyu.space.codegen.bean.vo.attr.AttrSearcheVo;
import com.yanyu.space.codegen.dao.AttrMapper;
import com.yanyu.space.codegen.service.IAttrService;
import com.yanyu.space.common.core.bean.db.PageEntity;
import com.yanyu.space.common.db.pagination.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板属性 服务实现类
 *
 * @author yanyu
 * @date 2020/11/18
 */
@Service
public class AttrServiceImpl extends ServiceImpl<AttrMapper, Attr> implements IAttrService {

    @Override
    public PageEntity<Attr> list(AttrSearcheVo vo) {
        // 0：全部，1：自定义，2：默认
        LambdaQueryWrapper<Attr> wrapper = Wrappers.<Attr>lambdaQuery()
                .like(StringUtils.isNotBlank(vo.getName()), Attr::getName, vo.getName())
                .eq(vo.getType() != null, Attr::getType, vo.getType())
                .orderByDesc(Attr::getType).orderByAsc(Attr::getSort);
        return PageUtil.getResult(baseMapper.selectPage(PageUtil.getParam(), wrapper));
    }

    @Override
    public Map<String, List<Attr>> listType() {
        List<Attr> defaultAttrs = baseMapper.selectList(Wrappers.<Attr>lambdaQuery()
                .eq(Attr::getType, 2).orderByAsc(Attr::getSort));

        List<Attr> customAttrs = baseMapper.selectList(Wrappers.<Attr>lambdaQuery().eq(Attr::getType, 1).orderByAsc(Attr::getSort));

        Map<String, List<Attr>> result = new HashMap<>(2);
        result.put("custom", customAttrs);
        result.put("default", defaultAttrs);
        return result;
    }
}
